package com.example.yoga.controller;


import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.example.yoga.entity.*;
import com.example.yoga.service.ICardCourseService;
import com.example.yoga.service.ICourseService;
import com.example.yoga.service.IUserCardService;
import com.example.yoga.service.IUserService;
import com.example.yoga.util.JsonResult;
import com.example.yoga.util.PageDataResult;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author liwenkai
 * @since 2021-03-11
 */
@RestController
@RequestMapping("/cardCourse")
public class CardCourseController {


    @Autowired
    private IUserCardService userCardService;

    @Resource
    private IUserService userService;

    @Autowired
    private ICardCourseService cardCourseService;

    @Autowired
    private ICourseService courseService;

    @RequestMapping("/getMyCourseList")
    public PageDataResult getMyCourseList(String name, Integer userid, Integer pageIndex, Integer pageSize){
        PageHelper.startPage(pageIndex,pageSize);
        //获取我的课程的列表
        //根据userid找到cardid
        QueryWrapper<UserCard> wrapper = new QueryWrapper<>();
        wrapper.eq("userId",userid);
        UserCard one = userCardService.getOne(wrapper);
        if(one==null){
            PageDataResult pdr = new PageDataResult();
            return null;
        }
        //找到cardid之后.再根据cardid找到该用户用该卡已购的所有课程
        QueryWrapper<CardCourse> cardCourseQueryWrapper = new QueryWrapper<>();
        cardCourseQueryWrapper.eq("cardId",one.getCardid());
        List<CardCourse> list = cardCourseService.list(cardCourseQueryWrapper);
        List list1=new ArrayList();
        for (int i = 0; i < list.size(); i++) {
            list1.add(list.get(i).getCourseid());
        }
        //获取到所有的课程id列表,找出课程名
        List<Course> courseList = courseService.listByIds(list1);
        if (courseList.size()>0){
            PageInfo<Course> pageInfo = new PageInfo<>(courseList);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        return null;
    }


    /**
     *getCourseVipList 获取某一课程下的vip列表
     */
    @RequestMapping("/getCourseVipList")
    public PageDataResult getCourseVipList(Integer courseid,Integer userid,Integer pageIndex,Integer pageSize){
        //根据courseid找到cardid,根据cardid找到userid,根据userid找到user
          //1.根据courseid找到cardid
        System.out.println(courseid);
        QueryWrapper<CardCourse> cardCourseQueryWrapper = new QueryWrapper<>();
        cardCourseQueryWrapper.eq("courseId",courseid);
        List<CardCourse> cardCourseList = cardCourseService.list(cardCourseQueryWrapper);
        System.out.println(cardCourseList);
        if (cardCourseList==null){
            return null;
        }
        List<Integer> idList = new ArrayList<Integer>();
        for (int i = 0; i < cardCourseList.size(); i++) {
            idList.add(cardCourseList.get(i).getCardid());
        }
        System.out.println(idList.toString());
        QueryWrapper<UserCard> userCardQueryWrapper = new QueryWrapper<>();
        userCardQueryWrapper.in("cardId",idList);
        List<UserCard> userCardList = userCardService.list(userCardQueryWrapper);
        System.out.println(userCardList.toString());
        idList.clear();
        for (UserCard userCard : userCardList) {
            if (!userid.equals(userCard.getUserid())){
                idList.add(userCard.getUserid());
            }
        }
        PageHelper.startPage(pageIndex,pageSize);
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("userId", idList);
        List<User> userList = userService.list(userQueryWrapper);
        System.err.println(userList);
        if (userList.size()>0){
            PageInfo<User> pageInfo = new PageInfo<>(userList);
            PageDataResult pdr = new PageDataResult();
            pdr.setList(pageInfo.getList());
            pdr.setTotals(Long.valueOf(pageInfo.getTotal()).intValue());
            return pdr;
        }
        //找出这个课程下的所有cardId
        //根据cardId找到userId
//        QueryWrapper<UserCard> wrapper = new QueryWrapper<>();
//        wrapper.eq("userId",userid);
//        UserCard one = userCardService.getOne(wrapper);
//        if(one==null){
//            PageDataResult pdr = new PageDataResult();
//            return null;
//        }
//        //找到cardid之后.再根据cardid找到该用户用该卡已购的所有课程
//        QueryWrapper<CardCourse> cardCourseQueryWrapper = new QueryWrapper<>();
//        cardCourseQueryWrapper.eq("cardId",one.getCardid());
//        List<CardCourse> list = cardCourseService.list(cardCourseQueryWrapper);
//        List list1=new ArrayList();
//        for (int i = 0; i < list.size(); i++) {
//            list1.add(list.get(i).getCourseid());
//        }
//        //获取到所有的课程id列表,找出课程名
//        List<Course> courseList = courseService.listByIds(list1);
        return null;
    }

    /**
     * exportCourseVip 导出某一课程下的所有vip
     */
    @RequestMapping(value="/exportCourseVip",method = RequestMethod.GET)
    public void exportCourseVip(HttpServletResponse response, Integer courseid,Integer userid) throws IOException {
        System.out.println("你要导出课程id为"+courseid+"用户id为"+userid);

        QueryWrapper<CardCourse> cardCourseQueryWrapper = new QueryWrapper<>();
        cardCourseQueryWrapper.eq("courseId",courseid);
        List<CardCourse> cardCourseList = cardCourseService.list(cardCourseQueryWrapper);
        System.out.println(cardCourseList);
        if (cardCourseList==null){
            System.out.println("cardCourseList为空！");
        }
        List<Integer> idList = new ArrayList<Integer>();
        for (int i = 0; i < cardCourseList.size(); i++) {
            idList.add(cardCourseList.get(i).getCardid());
        }
        QueryWrapper<UserCard> userCardQueryWrapper = new QueryWrapper<>();
        userCardQueryWrapper.in("cardId",idList);
        List<UserCard> userCardList = userCardService.list(userCardQueryWrapper);
        idList.clear();
        for (UserCard userCard : userCardList
        ) {
            if (!userid.equals(userCard.getUserid())){
                idList.add(userCard.getUserid());
            }
        }
        QueryWrapper<User> userQueryWrapper = new QueryWrapper<>();
        userQueryWrapper.in("userId", idList);
        List<User> userList = userService.list(userQueryWrapper);
        String fileName = courseid+"号课程vip名单.xls";
        try {
            response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
            response.setContentType("application/vnd.ms-excel;charset=utf-8");
            //导出
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams(), User.class, userList);
            workbook.write(response.getOutputStream());
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            return;
        }


    }


}

